{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 合并编译器区域\n",
    "\n",
    "参考：`tvm/tests/python/relay/test_pass_merge_compiler_regions.py`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "{func}`~tvm.relay.op.annotation.compiler_begin` 和 {func}`~tvm.relay.op.annotation.compiler_end` 函数的主要作用是在 Relay 表达式中标记区域的开始和结束，该区域将由指定的编译器处理。这个函数通常用于优化和调度，允许不同的编译器处理不同的表达式区域。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tvm\n",
    "from tvm import relay\n",
    "import tvm.relay.transform as transform\n",
    "from tvm.relay.op.annotation import compiler_begin, compiler_end\n",
    "from tvm.relay.testing import run_opt_pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 处理菱形区域"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在菱形计算图中存在的数据依赖关系是否被合并过程正确解决。\n",
    "\n",
    "````\n",
    "    O = 目标支持\n",
    "    X = 目标不支持\n",
    "\n",
    "       O         O\n",
    "      / \\\\      /               \\\\\n",
    "     O   X --> O    +       +    X\n",
    "     \\\\ /             \\\\ /\n",
    "       O                O\n",
    "````\n",
    "\n",
    "注意不能简单地将三个支持的算子合并在一起，否则两个子图都会依赖于另一个。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def diamond_graph_fanouts():\n",
    "    data = relay.var(\"data\", shape=(10, 10))\n",
    "    cb_1 = compiler_begin(data, \"test\")\n",
    "    O_1 = relay.abs(cb_1)\n",
    "    ce_1 = compiler_end(O_1, \"test\")\n",
    "    ce_2 = compiler_end(O_1, \"test\")\n",
    "    cb_2 = compiler_begin(ce_1, \"test\")\n",
    "    cb_3 = compiler_begin(ce_2, \"default\")\n",
    "    O_2 = relay.nn.relu(cb_2)\n",
    "    ce_3 = compiler_end(O_2, \"test\")\n",
    "\n",
    "    X = relay.tanh(cb_3)\n",
    "    ce_4 = compiler_end(X, \"default\")\n",
    "\n",
    "    cb_4 = compiler_begin(ce_3, \"test\")\n",
    "    cb_5 = compiler_begin(ce_4, \"test\")\n",
    "    O_3 = relay.add(cb_4, cb_5)\n",
    "    ce_5 = compiler_end(O_3, \"test\")\n",
    "\n",
    "    diamond = relay.Function([data], ce_5)\n",
    "    return diamond"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"highlight\" style=\"background: \"><pre style=\"line-height: 125%;\"><span></span><span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #AA22FF\">@main</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>data: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32]) {\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>data, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> abs(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> nn<span style=\"color: #AA22FF; font-weight: bold\">.</span>relu(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> tanh(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">10</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">11</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">12</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">10</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">11</span>);\n",
       "  annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">12</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>)\n",
       "}\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "expr = diamond_graph_fanouts()\n",
    "tvm.IRModule.from_expr(expr).show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"highlight\" style=\"background: \"><pre style=\"line-height: 125%;\"><span></span><span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #AA22FF\">@main</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>data: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>) <span style=\"color: #AA22FF; font-weight: bold\">-&gt;</span> Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] {\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>data, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> abs(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> nn<span style=\"color: #AA22FF; font-weight: bold\">.</span>relu(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> tanh(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">10</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">10</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>\n",
       "}\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "opt_expr = run_opt_pass(expr, relay.transform.MergeCompilerRegions())\n",
    "tvm.IRModule.from_expr(opt_expr).show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算图示例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参见 [RFC 5830](https://discuss.tvm.apache.org/t/relay-improved-graph-partitioning-algorithm/5830) 蓝色节点是加法（目标：测试），红色节点是减法（目标：默认）。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原始计算图："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": [
     "hide-cell"
    ]
   },
   "outputs": [],
   "source": [
    "def annotated():\n",
    "    in_1 = relay.var(\"in_1\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_2 = relay.var(\"in_2\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_3 = relay.var(\"in_3\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_4 = relay.var(\"in_4\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_5 = relay.var(\"in_5\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_6 = relay.var(\"in_6\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_7 = relay.var(\"in_7\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_8 = relay.var(\"in_8\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_9 = relay.var(\"in_9\", shape=(10, 10), dtype=\"float32\")\n",
    "    in_10 = relay.var(\"in_10\", shape=(10, 10), dtype=\"float32\")\n",
    "\n",
    "    begin0 = compiler_begin(in_1, \"test\")\n",
    "    begin1 = compiler_begin(in_2, \"test\")\n",
    "    begin2 = compiler_begin(in_3, \"test\")\n",
    "    begin3 = compiler_begin(in_4, \"test\")\n",
    "    node0 = relay.add(begin0, begin1)\n",
    "    node1 = relay.add(begin2, begin3)\n",
    "    end0 = compiler_end(node0, \"test\")\n",
    "    end1 = compiler_end(node1, \"test\")\n",
    "    begin4 = compiler_begin(end0, \"test\")\n",
    "    begin5 = compiler_begin(end1, \"test\")\n",
    "    node2 = relay.add(begin4, begin5)\n",
    "    end2 = compiler_end(node2, \"test\")\n",
    "\n",
    "    dbegin0 = compiler_begin(in_5, \"default\")\n",
    "    dbegin1 = compiler_begin(in_6, \"default\")\n",
    "    node3 = relay.subtract(dbegin0, dbegin1)\n",
    "    dbegin2 = compiler_begin(in_7, \"default\")\n",
    "    dend1 = compiler_end(node3, \"default\")\n",
    "    dbegin3 = compiler_begin(dend1, \"default\")\n",
    "    node4 = relay.subtract(dbegin2, dbegin3)\n",
    "    dend2 = compiler_end(node4, \"default\")\n",
    "\n",
    "    begin6 = compiler_begin(end2, \"test\")\n",
    "    begin7 = compiler_begin(dend2, \"test\")\n",
    "    node5 = relay.add(begin6, begin7)\n",
    "    end3 = compiler_end(node5, \"test\")\n",
    "    end4 = compiler_end(node5, \"test\")\n",
    "    dbegin4 = compiler_begin(in_8, \"default\")\n",
    "    dbegin5 = compiler_begin(end3, \"default\")\n",
    "    node6 = relay.subtract(dbegin4, dbegin5)\n",
    "    begin8 = compiler_begin(in_9, \"test\")\n",
    "    begin9 = compiler_begin(end4, \"test\")\n",
    "    node7 = relay.add(begin8, begin9)\n",
    "    end5 = compiler_end(node7, \"test\")\n",
    "\n",
    "    dend3 = compiler_end(node6, \"default\")\n",
    "    begin10 = compiler_begin(dend3, \"test\")\n",
    "    begin11 = compiler_begin(end5, \"test\")\n",
    "    node8 = relay.add(begin10, begin11)\n",
    "    end6 = compiler_end(node8, \"test\")\n",
    "    begin12 = compiler_begin(in_10, \"test\")\n",
    "    begin13 = compiler_begin(end6, \"test\")\n",
    "    node9 = relay.add(begin12, begin13)\n",
    "    end7 = compiler_end(node9, \"test\")\n",
    "\n",
    "    f = relay.Function([in_1, in_2, in_3, in_4, in_5, in_6, in_7, in_8, in_9, in_10], end7)\n",
    "    mod = tvm.IRModule.from_expr(f)\n",
    "    return mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"highlight\" style=\"background: \"><pre style=\"line-height: 125%;\"><span></span><span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #AA22FF\">@main</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_1: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_2: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_3: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_4: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_5: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_6: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_7: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_8: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_9: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32], <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_10: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32]) {\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_1, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_2, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_3, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_4, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">10</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">11</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">10</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">12</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_5, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">13</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_6, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">14</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> subtract(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">12</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">13</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">15</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">14</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">16</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_7, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">17</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">15</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">18</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> subtract(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">16</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">17</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">19</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">18</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">20</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">11</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">21</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">19</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">22</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">20</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">21</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">23</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">22</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">24</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_8, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">25</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">23</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">26</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> subtract(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">24</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">25</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">27</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">26</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">28</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">22</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">29</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_9, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">30</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">28</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">31</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">29</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">30</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">32</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">31</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">33</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">27</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">34</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">32</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">35</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">33</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">34</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">36</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">35</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">37</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_10, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">38</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">36</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>);\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">39</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">37</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">38</span>);\n",
       "  annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">39</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>)\n",
       "}\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "mod = annotated()\n",
    "mod.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "优化之后的计算图："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": [
     "hide-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"highlight\" style=\"background: \"><pre style=\"line-height: 125%;\"><span></span><span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #AA22FF\">@main</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_1: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_2: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_3: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_4: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_5: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_6: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_7: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_8: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_9: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>in_10: Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>) <span style=\"color: #AA22FF; font-weight: bold\">-&gt;</span> Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] {\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_1, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_2, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_3, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_4, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_5, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_6, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_7, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> subtract(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">10</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> subtract(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">11</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">10</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">12</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">13</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">11</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">14</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">12</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">13</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">15</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">14</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">16</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_8, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">17</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">15</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">18</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> subtract(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">16</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">17</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">19</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">18</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;default&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">20</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_9, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">21</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">20</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">14</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">22</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">21</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">23</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">19</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">24</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">22</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">25</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_begin(<span style=\"color: #AA22FF; font-weight: bold\">%</span>in_10, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">26</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">23</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">24</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">27</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">25</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">26</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  annotation<span style=\"color: #AA22FF; font-weight: bold\">.</span>compiler_end(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">27</span>, compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">10</span>, <span style=\"color: #008000\">10</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>\n",
       "}\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "mod = relay.transform.MergeCompilerRegions()(mod)\n",
    "mod = relay.transform.InferType()(mod)\n",
    "mod.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试条件语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个测试了限制区域在 if_else 控制流中成功传播。\n",
    "\n",
    "```\n",
    "    O = 目标支持\n",
    "    X = 目标不支持\n",
    "\n",
    "           O1 - - - |      O1 --|\n",
    "            |       |               |\n",
    "            X       |               X\n",
    "            |       |                              |\n",
    "    If cond ? O1: X | -->       +       +  If cond ? O1: X  +\n",
    "            |       |                                           |\n",
    "           O2 <- - -|                                          O2 <-|\n",
    "\n",
    "```\n",
    "    \n",
    "避免 O1 合并到 O2。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = \"test_if_else_merge\"\n",
    "\n",
    "@tvm.ir.register_op_attr(\"sigmoid\", \"target.\" + target)\n",
    "def sigmoid(expr):  # pylint: disable=unused-variable\n",
    "    return True\n",
    "\n",
    "@tvm.ir.register_op_attr(\"erf\", \"target.\" + target)\n",
    "def erf(expr):  # pylint: disable=unused-variable\n",
    "    return True\n",
    "\n",
    "@tvm.ir.register_op_attr(\"add\", \"target.\" + target)\n",
    "def add(expr):  # pylint: disable=unused-variable\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_mod():\n",
    "    data = relay.var(\"data\", shape=(1, 32))\n",
    "    add0 = relay.add(data, data)\n",
    "    sub0 = relay.subtract(add0, data)\n",
    "    eq = relay.equal(relay.sum(add0), relay.sum(sub0))\n",
    "\n",
    "    true_branch = relay.sigmoid(add0)\n",
    "    false_branch = relay.sigmoid(sub0)\n",
    "    ife = relay.If(eq, true_branch, false_branch)\n",
    "    erf = relay.erf(ife)\n",
    "    out = relay.add(add0, erf)\n",
    "    func = relay.Function([data], out)\n",
    "    mod = tvm.IRModule.from_expr(func)\n",
    "\n",
    "    return mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "for annotate_non_call_ops in [True, False]:\n",
    "    result = transform.AnnotateTarget(target, annotate_non_call_ops)(get_mod())\n",
    "    merge = transform.MergeCompilerRegions()(result)\n",
    "    # 确保分区完成而没有段错误。\n",
    "    partition = transform.PartitionGraph()(merge)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"highlight\" style=\"background: \"><pre style=\"line-height: 125%;\"><span></span><span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #AA22FF\">@main</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>data: Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>) <span style=\"color: #AA22FF; font-weight: bold\">-&gt;</span> Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] {\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> <span style=\"color: #AA22FF\">@tvmgen_default_test_if_else_merge_main_0</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>data) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>(Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32], Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32]) <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0.0</span> <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> subtract(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>data) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> sum(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>float32 <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> sum(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>float32 <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> equal(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">3</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">4</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>bool <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> <span style=\"color: #008000; font-weight: bold\">if</span> (<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">5</span>) {\n",
       "    <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">0.1</span> <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>\n",
       "  } <span style=\"color: #008000; font-weight: bold\">else</span> {\n",
       "    <span style=\"color: #AA22FF\">@tvmgen_default_test_if_else_merge_main_3</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">2</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>\n",
       "  };\n",
       "  <span style=\"color: #AA22FF\">@tvmgen_default_test_if_else_merge_main_2</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">1</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">6</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>\n",
       "}\n",
       "\n",
       "<span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #AA22FF\">@tvmgen_default_test_if_else_merge_main_0</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_0_i0: Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, Compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test_if_else_merge&quot;</span>, Primitive<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #008000\">1</span>, Inline<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #008000\">1</span>, global_symbol<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;tvmgen_default_test_if_else_merge_main_0&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">-&gt;</span> (Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32], Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32]) {\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> add(<span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_0_i0, <span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_0_i0) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> sigmoid(<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  (<span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">7</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">8</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>(Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32], Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32]) <span style=\"color: #AA22FF; font-weight: bold\">*/</span>\n",
       "}\n",
       "\n",
       "<span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #AA22FF\">@tvmgen_default_test_if_else_merge_main_2</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_2_i0: Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, <span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_2_i1: Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, Compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test_if_else_merge&quot;</span>, Primitive<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #008000\">1</span>, Inline<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #008000\">1</span>, global_symbol<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;tvmgen_default_test_if_else_merge_main_2&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">-&gt;</span> Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] {\n",
       "  <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span> <span style=\"color: #AA22FF; font-weight: bold\">=</span> erf(<span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_2_i1) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>;\n",
       "  add(<span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_2_i0, <span style=\"color: #AA22FF; font-weight: bold\">%</span><span style=\"color: #008000\">9</span>) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>\n",
       "}\n",
       "\n",
       "<span style=\"color: #008000; font-weight: bold\">def</span> <span style=\"color: #AA22FF\">@tvmgen_default_test_if_else_merge_main_3</span>(<span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_3_i0: Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>, Compiler<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;test_if_else_merge&quot;</span>, Primitive<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #008000\">1</span>, Inline<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #008000\">1</span>, global_symbol<span style=\"color: #AA22FF; font-weight: bold\">=</span><span style=\"color: #BA2121\">&quot;tvmgen_default_test_if_else_merge_main_3&quot;</span>) <span style=\"color: #AA22FF; font-weight: bold\">-&gt;</span> Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] {\n",
       "  sigmoid(<span style=\"color: #AA22FF; font-weight: bold\">%</span>test_if_else_merge_3_i0) <span style=\"color: #AA22FF; font-weight: bold\">/*</span> ty<span style=\"color: #AA22FF; font-weight: bold\">=</span>Tensor[(<span style=\"color: #008000\">1</span>, <span style=\"color: #008000\">32</span>), float32] <span style=\"color: #AA22FF; font-weight: bold\">*/</span>\n",
       "}\n",
       "</pre></div>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "partition.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
